<?php
namespace Admin\Controller;
use Admin\Common\Page;
class RecycleController extends BaseController
{
    /**
     * 回收库包列表
     */
    public function index(){
        $db = M('');
        $key = I('get.keywords');
        if($key != ""){
            $map = " and b.bag_card like '%{$key}%'";
        }else{
            $map = "";
        }
        $count = M('bag as b')->where('b.status=2'.$map)->count();
        $Page = new  Page($count , C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $data = $db->table('bag as b,student as s,class,child_school as c,user as u')
            ->where("b.status=2 and b.stu_id=s.stu_id and s.class_id=class.id and  class.child_school_id=c.id and s.user_id=u.user_id".$map)
            ->field("b.remark,b.bag_id,b.bag_card,class.class_name,c.name,u.nickname,b.bag_name")
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $this->assign('page',$show);
        $this->assign('data',$data);
        $this->display();
    }

    /**
     * 解锁
     */
    public function recovery(){
        $bag_id = I('get.bag_id');
        if($bag_id == ""){
            $this->error('参数错误');
        }
        $db = M();
        $db->startTrans();
        $stuId = $db->table('bag')->where('bag_id='.$bag_id)->getField('stu_id');
        /**
         * 1.将书包与学生之间断开关系
         */
        $key1 = $db->table('bag')->where('bag_id='.$bag_id)->save(array('stu_id'=>0,'status'=>0));
        $key2 = $db->table('student')->where('stu_id='.$stuId)->save(array('status'=>0,'bag_id'=>0));

        /**
         * 2.改变这些书的状态
         */
        $bookIds = $db->table('bag_book')->where('status=0 and bag_id='.$bag_id)->getField('book_id',true);
        $link = D('book')->bookStatus($bookIds,4);
        if($link == 400){
            $this->error('更改图书状态出错');
        }
        if($key2 && $key1){
            $db->commit();
            $link->commit();
            $this->success('解锁成功',U('index'));
        }else{
            $db->rollback();
            $this->error('解锁失败');
        }
    }

    /**
     * 回收图书
     */
    public function book(){
        $count = M('recover_book')->where('status=1')->count();
        $Page = new  Page($count , C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $db = M();
        $data = $db->table('recover_book as a,book as b')
            ->where('a.status=0 and a.book_id=b.book_id')
            ->field('a.*,b.sm')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $this->assign('page',$show);
        $this->assign('data',$data);
        $this->display();
    }

    /**
     * 图书报废
     */
    public function baoFei(){
        $id = I('get.id');
        if($id == ""){
            $this->error('参数错误');
        }

        $db = M();
        $data = $db->table('recover_book')->where('id='.$id)->find();
        $data['status'] = 4;
        $key = $db->table('recover_book')->save($data);
        $link = D('book')->bookStatus(array($data['book_id']),8);
        if($link == 400){
            $db->rollback();
            $this->error('图书状态修改失败');
        }
        if($key){
            $db->commit();
            $link->commit();
            $this->success('报废成功',U('book'));
        }else{
            $link->rollback();
            $db->rollback();
            $this->error('报废失败');
        }
    }

    /**
     * 书包借出历史
     */
    public function log(){
        $db = M('');
        $map = "";
        if(isset($_GET['bag_card']) && $_GET['bag_card'] != ""){
            $map .=" and b.bag_card like '%{$_GET['bag_card']}%'";
        }
        if(isset($_GET['tm']) && $_GET['tm'] != ""){
            $map .=" and b.book_tm like '%{$_GET['tm']}%'";
        }
        $count = M('card as a,bag_log as b')->where('b.card_id=a.card_id'.$map)->count();
        $Page = new  Page($count , C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出

        $data = $db->table('card as a,bag_log as b')
            ->where('b.card_id=a.card_id'.$map)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->field('b.*,a.child_name')
            ->select();
       // dump($data);die;
        $this->assign('page',$show);
        $this->assign('data',$data);
        $this->display();
    }



}

